package com.yunpuvip.sachet.modular.constant.response;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.yunpuvip.sachet.modular.core.exception.ServiceExceptionEnum;

import java.io.Serializable;

//保证序列化Json的时候,如果是null的对象,Key也会消失
//@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T>  implements Serializable {

    private int code;

    private String message;

    private T data;


    private ServerResponse(){}

    private ServerResponse(int status){
        this.code=status;
    }

    private ServerResponse(int status, String msg){
        this.code=status;
        this.message=msg;
    }

    public ServerResponse(int status, String msg, T data){
        this.code=status;
        this.message=msg;
        this.data=data;
    }

    private ServerResponse(int status, T data){
        this.code=status;
        this.data=data;
    }


    /**
     * success:true;
     *
     * @Param: []
     * @return: boolean
     * @Author: hyf
     * @Date: 2018/7/18
     */
    //使之不在Json序列化结果之中 @JsonIgnore
    @JSONField(serialize=false)
    public   boolean  isSuccess(){
        return this.code==ResponseCode.SUCCESS.getCode();
    }


    /**
     * error:true
     *
     * @Param: []
     * @return: boolean
     * @Author: hyf
     * @Date: 2018/7/18
     */
    @JSONField(serialize = false)
    public boolean isError() {
        return this.code != ResponseCode.SUCCESS.getCode();
    }


    public static <T> ServerResponse<T> creatByResult(boolean result){
        if(result){
            return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
        }else{
            return new ServerResponse<T>(ResponseCode.ERROR.getCode());
        }
    }


    public static <T> ServerResponse<T> creatBySuccess(){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public   static <T> ServerResponse<T> creatBySuccessMessage(String msg){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public   static <T> ServerResponse<T> creatBySuccess(T data){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    public   static <T> ServerResponse<T> creatBySuccess(String msg,T data){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }

    public   static <T> ServerResponse<T> creatByError(){
        return  new ServerResponse<T>(ResponseCode.ERROR.getCode());
    }
    public   static <T> ServerResponse<T> creatByErrorMsg(String errorMsg){
        return  new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMsg);
    }

    public   static <T> ServerResponse<T> creatByErrorCodeMsg(int errorCode,String errorMsg){
        return  new ServerResponse<T>(errorCode,errorMsg);
    }


    public   static <T> ServerResponse<T> creatByErrorEnum(ServiceExceptionEnum exceptionEnum){
        return  new ServerResponse<T>(exceptionEnum.getCode(),exceptionEnum.getMessage());
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
